﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using OpenWeb.Dal.Models;
using WebMatrix.WebData;

namespace OpenWeb.Dal.DbCreation
{
  
 public class DbInitializer
    {
   public DbInitializer()
      {
        //Database.SetInitializer<UsersContext>(
        // new System.Data.Entity.DropCreateDatabaseIfModelChanges<UsersContext>()); 

        //Database.SetInitializer<ApplicationDbEntities>(
        // new OpenWeb.Dal.DbCreation.CreatePocoDb<ApplicationDbEntities>(CreationEnum.CreateOnlyIfModelChange, true));
        
   }

   public void Init(CreationEnum pCondizione)
   {
        bool dbexists = false ;

        try 	
        {


            using (var context = new ApplicationDbEntities())
            { 

                try 
	                {	        
		
                  dbexists=context.Database.Exists();
                      

                        switch (pCondizione)
                        {
                          case CreationEnum.DropCreateAlways:
                            context.Database.Delete();
                            break;
                          case CreationEnum.CreateOnlyIfNotExists:
                            if (dbexists)
                              return;
                            break;
                          case CreationEnum.CreateOnlyIfModelChange:
                            if (dbexists)                
                                if(!context.Database.CompatibleWithModel(true))
                                    context.Database.Delete();
                            else
                              return;
                            break;
                          default:
                            break;
                        }

                      context.Database.Initialize(true);
	                }
	                catch (Exception ex)
	                {
		                if(dbexists)
		                    context.Database.Initialize(true);
	                }

            }

            

           //if (!context.Database.Exists() )
           // {
           //   //context.Database.Initialize(true);// true : senza attesa di chiamate al db
           //   // Create the SimpleMembership database without Entity Framework migration schema
           //   ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
           // }
           // else if (Debugger.IsAttached & !context.Database.CompatibleWithModel(true))
           // {
           //   context.Database.Delete();
           //   //context.Database.Initialize(true);
           //   /*
           //    * context.Database.Delete();
           //     context.Database.Create();
           //    * */
           // }
            
          

          //WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName",
          //autoCreateTables: true);
        }
        catch (Exception ex)
        {
          throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
        }
      }






    }
}
